package org.fitweb.runners;

import fitnesse.trinidad.InMemoryTestImpl;
import fitnesse.trinidad.TestDescriptor;
import fitnesse.trinidad.TestRepository;
import fitnesse.trinidad.TestResultRepository;
import static org.apache.commons.io.FileUtils.readFileToString;

import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * Pretends to be a FitnesseRepository, so that we can use Trinidad to run tests written in plain HTML, not wiki markup.
 */
public class FitNotFitnesseRepository implements TestRepository {

    public void prepareResultRepository(TestResultRepository resultRepository) throws IOException {
        // Don't bother - nothing to prepare
    }

    public void setUri(String uri) throws IOException {
        // Again, don't bother
    }

    public TestDescriptor getTest(String url) throws IOException {
        File testFile = new File(url);
        return new InMemoryTestImpl(stripExtension(testFile.getName()), readFileToString(testFile));
    }

    public List<TestDescriptor> getSuite(String name) throws IOException {
        throw new UnsupportedOperationException("Haven't added support for suites yet");
    }

    private String stripExtension(String name) {
        return name.replaceAll("\\.html", "");
    }
}